package cn.xfdzx.android.apps.shop.util;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.annotation.Nullable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by zf on 2018/11/01.
 */

public class FileUtils {

    /**
     * 生成文件夹路径
     */
    public static String SDPATH = Environment.getExternalStorageDirectory() + "/zdphoto/";

    /**
     * @param contentUri
     * @param context
     * @return
     */
    public static String getRealPathFromURI(Uri contentUri, Context context) {
        String res = null;
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        if (null != cursor && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(columnIndex);
            cursor.close();
        }
        return res;
    }

    @Nullable
    private String cretateBitmapPath(Bitmap photo) {
        String path;
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }
        File wsPhoto = new File(Environment.getExternalStorageDirectory(), "teacherHelp");
        if (!wsPhoto.exists()) {
            wsPhoto.mkdir();
        }
        File photoFile = new File(wsPhoto.getAbsolutePath() +
                File.separator + System.currentTimeMillis() + ".png");
        FileOutputStream fos = null;
        try {
            photoFile.createNewFile();
            fos = new FileOutputStream(photoFile);
            photo.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            photoFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
            photoFile.delete();
        } finally {
            try {
                if (null != fos) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (!photoFile.exists()) {
            return null;
        }
        path = photoFile.getAbsolutePath();
        return path;
    }

//判断文件是否存在

    public static  boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {

            return false;

        }
        return true;

    }

    //在这里抽取了一个方法   可以封装到自己的工具类中...
    public static File getFile(Bitmap bitmap,String filename) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos);
        File file = new File(Environment.getExternalStorageDirectory() + "/"+filename+".jpg");
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            InputStream is = new ByteArrayInputStream(baos.toByteArray());
            int x = 0;
            byte[] b = new byte[1024 * 100];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

}
